package com.example.nekoneko.controller;

import cn.hutool.core.util.StrUtil;
import com.example.nekoneko.common.AuthAccess;
import com.example.nekoneko.common.Result;
import com.example.nekoneko.dao.User;
import com.example.nekoneko.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/*功能：提供接口返回数据*/
@RestController
public class WebController {
    @Resource//装配Bean
    UserService userService;
    @AuthAccess
    @GetMapping("/")
    public Result hello(){
        return Result.success();
    }
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        if (StrUtil.isBlank(user.getUsername()) ||
        StrUtil.isBlank(user.getUsername())){
            return Result.error("数据输入不合法");
        }
        user = userService.login(user);
        return Result.success(user);
    }

    /*注册*/
    @AuthAccess
    @PostMapping("/register")
    public Result register(@RequestBody  User user){
        if (StrUtil.isBlank(user.getUsername()) ||
        StrUtil.isBlank(user.getPassword())){
            return Result.error("数据输入不合法");
        }
        if (user.getUsername().length()>10 || user.getPassword().length()>20){
            return Result.error("数据输入不合法");
        }
        user = userService.login(user);
        return Result.success(user);
    }
    /*重置密码*/
    @AuthAccess
    @PostMapping("/password")
    public Result password(@RequestBody User user){
        if (StrUtil.isBlank(user.getUsername())){
            return Result.error("数据输入不合法");
        }
        userService.resetPassword(user);
        return Result.success();
    }
}
